<?

// u1 add u2,u3,u4 as a friends
// u1 has commented on u2,u3 profile
//	Prado::using('System.utils.*');
 Prado::using('Application.pages.utils.DateUtils');
 Prado::using('Application.pages.utils.CommonUtils');

class ActivityRenderer extends TRepeaterItemRenderer
{
	public $serverPathDownload = "assets/images/";
	
	public function onInit($param)
    {	
//		parent::onInit($param);
		
		$this->setActivityString($this->Data->activity_id);
		
	}
	public function getFriendName($friend_id)
	{
		
		$friend_name = UserRecord::finder()->findByPk($friend_id);
		
		//echo "sssss ". $this;
		//$u_id = $owner->user_id;
		return $friend_name->username;
	}
	
	public function setActivityString($activity_id)
	{
		
		if($activity_id == 0)
		{	
			$this->activityItems->Text = $this->getFriendsText();
			//return " are now friends";
		}
		else if($activity_id == 1)
		{
			$this->activityItems->Text = $this->getCommentsProfileText();
		}
		else if($activity_id == 2) // update profile
		{
			$this->activityItems->Text = $this->getUpdateProfileText();
		}
		else if($activity_id == 3) //update status
		{
			$this->activityItems->Text = $this->getUpdateStatusText();
		}
		else if($activity_id == 4) //update display name
		{
			$this->activityItems->Text = $this->getUpdateDisplayNameText();
		}
	}
	
	public function getUserRecord($id)
	{
	
		$userRecord = UserRecord::finder()->findByUser_Id($id);
		return $userRecord;
	}
	
	public function getUpdateDisplayNameText()
	{
		$rec = UpdateStatusRecord::finder()->findByPk($this->Data->rec_id);
		
		$ret = "";
		$ret = $ret . "<img class=\"avatar\"  src=\"" .$this->getAvatar($this->getUserRecord($rec->user_id)) . "\">";
		$ret = $ret . "<span class = \"contentBox\">";
		$ret = $ret . "<span class = \"postInformation\">";
		$ret = $ret . "<span class = \"posterName\">";
		$ret = $ret . "<a href= " . $this->Service->constructUrl('users.ProfileUser',array('id' => $rec->user_id  )) . ">"
			. $this->getFriendName($rec->user_id) . "</a>";
		$ret = $ret . "</span>";
		$ret = $ret . " has set new display name ";
		$ret = $ret . $this->getTimeDiff($rec->setstatus_datetime);
		$ret = $ret . "</span>";
		$ret = $ret . "<br /><br /> >>>> " . $rec->content. "<br />";
		$ret = $ret . "</span>";
		
		//$ret = $ret .  $this->getSeperator();
		return $ret;
	}
	
	public function getUpdateStatusText()
	{
		$rec = UpdateStatusRecord::finder()->findByPk($this->Data->rec_id);
		
		$ret = "";
		$ret = $ret . "<img class=\"avatar\"  src=\"" .$this->getAvatar($this->getUserRecord($rec->user_id)) . "\">";
		$ret = $ret . "<span class = \"contentBox\">";
		$ret = $ret . "<span class = \"postInformation\">";
		$ret = $ret . "<span class = \"posterName\">";
		$ret = $ret . "<a href= " . $this->Service->constructUrl('users.ProfileUser',array('id' => $rec->user_id  )) . ">"
			. $this->getFriendName($rec->user_id) . "</a>";
		$ret = $ret . "</span>";
		$ret = $ret . " has just wrote new recent activity ";
		$ret = $ret . $this->getTimeDiff($rec->setstatus_datetime);
		$ret = $ret . "</span>";
		$ret = $ret . "<br /><br /><div class = \"postText\"><span>" . $rec->content. "</span></div><br />";
		$ret = $ret . "</span>";

//		$ret = $ret .  $this->getSeperator();
		return $ret;
	}
	public function getCommentsProfileText()
	{	
		$profileComment_rec = ProfileCommentsRecord::finder()->findByPk($this->Data->rec_id);
		
		$ret = "";
		$ret = $ret . "<img class=\"avatar\"  src=\"" .$this->getAvatar($this->getUserRecord($profileComment_rec->commentator_user_id)) . "\">";
		$ret = $ret . "<span class = \"contentBox\">";
		$ret = $ret . "<span class = \"postInformation\">";
		$ret =  $ret ."<span class = \"posterName\"><a href= " . $this->Service->constructUrl('users.ProfileUser',array('id' => $profileComment_rec->commentator_user_id  )) . ">"
			. $this->getFriendName($profileComment_rec->commentator_user_id) . "</a></span>";
	
		$ret = $ret . " has commented on ";
		$ret = $ret . "<span class = \"posterName\"><a href= " . $this->Service->constructUrl('users.ProfileUser',array('id' => $profileComment_rec->owner_user_id  )) . ">"
			. $this->getFriendName($profileComment_rec->owner_user_id) . "</a></span>";
		$ret = $ret . "'s profile ";	
		$ret = $ret . $this->getTimeDiff($profileComment_rec->comment_datetime);
		$ret = $ret . "</span>";
		$ret = $ret . "</span>";
		return $ret;
	}
	
	public function getFriendsText()
	{	$friend_rec = FriendRecord::finder()->findByPk($this->Data->rec_id);
		
		$ret = "";
		$ret = $ret . "<img class=\"avatar\"  src=\"" .$this->getAvatar($this->getUserRecord($friend_rec->user_id)) . "\">";
		$ret = $ret . "<span class = \"contentBox\">";
		$ret = $ret . "<span class = \"postInformation\">";
		$ret = $ret . "<span class = \"posterName\"><a href= " . $this->Service->constructUrl('users.ProfileUser',array('id' => $friend_rec->user_id  )) . ">"
			. $this->getFriendName($friend_rec->user_id) . "</a></span>";
		$ret = $ret . " add ";
		$ret = $ret . "<span class = \"posterName\"><a href= " . $this->Service->constructUrl('users.ProfileUser',array('id' => $friend_rec->friend_id  )) . ">"
			. $this->getFriendName($friend_rec->friend_id) . "</a></span>";
		$ret = $ret . " as a friends ";	
		$ret = $ret . $this->getTimeDiff($friend_rec->friends_datetime);
		$ret = $ret . "</span>";
		$ret = $ret . "</span>";
		return $ret;
		
			
	}
	public function getUpdateProfileText()
	{
		$updateprofile_rec = UpdateProfileRecord::finder()->findByPk($this->Data->rec_id);
		
		$ret = "";
		$ret = $ret . "<img class=\"avatar\"  src=\"" .$this->getAvatar($this->getUserRecord($updateprofile_rec->user_id )) . "\">";
		$ret = $ret . "<span class = \"contentBox\">";
		$ret = $ret . "<span class = \"postInformation\">";
		$ret = $ret . "<span class = \"posterName\"><a href= " . $this->Service->constructUrl('users.ProfileUser',array('id' => $updateprofile_rec->user_id  )) . ">"
			. $this->getFriendName($updateprofile_rec->user_id) . "</a></span>";
		$ret = $ret . " has updated their profile on ";
		$ret = $ret . $this->getTimeDiff($updateprofile_rec->update_datetime);
		$ret = $ret . "</span>";
		$ret = $ret . "</span>";
		return $ret;
	}
	
	public function getTimeDiff($data)
	{
		return DateUtils::process_datetime($data);
	}
	
	public function getSeperator()
	{
		$ret = "<hr color=\"#CCCCCC\" size=\"1\" width=\"300\" align=\"left\"><br />";
		return $ret;
	}
	
	
	public function getAvatar($user)
	{
		$img_path = $this->Application->Parameters['ImageFolderName'];
		return CommonUtils::getAvatar($img_path,$user);	
	}
}

?>